//
//  XLLLoginView.m
//  XLLogin
//
//  Created by 陈宪栋 on 2017/11/15.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLLLoginView.h"
#import <UtilsMacro.h>
#import <Masonry.h>
#import <UIImage+Color.h>
#import <WXApi.h>

@implementation XLLLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kClearColor;
        self.frame = frame;
        
//        UIButton *back = [[UIButton alloc]init];
//        [back setImage:[UIImage imageNamed:@"common_nav_btn_back_n"] forState:UIControlStateNormal];
//        [self addSubview:back];
//        [back addTarget:self action:@selector(backButton1) forControlEvents:UIControlEventTouchUpInside];
//        [back mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.mas_left).offset(15);
//            make.top.equalTo(self.mas_top).offset(20);
//            make.width.mas_equalTo(50);
//            make.height.mas_equalTo(50);
//        }];
//
//        UILabel *tipLabel = [[UILabel alloc] init];
//        tipLabel.backgroundColor = [UIColor whiteColor];
//        tipLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightBold];
//        tipLabel.textColor = HEXCOLOR(0x000000);
//        tipLabel.text = @"手机验证";
//        [self addSubview:tipLabel];
//        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.mas_left).offset(35);
//            make.top.equalTo(self.mas_top).offset(100);
//        }];
        
        self.phoneBgView = [[UIImageView alloc]init];
        self.phoneBgView.backgroundColor = kClearColor;
        [self addSubview:self.phoneBgView];
        [self.phoneBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            make.top.equalTo(self.mas_top).offset(0);
            make.height.mas_equalTo(64);
        }];
        
        self.codeBtn = [[UIButton alloc]init];
        [self.codeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
        [self.codeBtn setTitleColor:RGB_HEX(0xffbb01) forState:UIControlStateNormal];
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:self.codeBtn];
        [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.equalTo(self.phoneBgView);
            make.right.equalTo(self.phoneBgView.mas_right).offset(-15);
            make.width.mas_equalTo(120);
        }];
        [[UITextField appearance] setTintColor:RGB_HEX(0xffbb01)];
        self.phoneField = [[UITextField alloc] initWithFrame:self.phoneBgView.frame];
        self.phoneField.backgroundColor = [UIColor clearColor];
        self.phoneField.textColor = [UIColor whiteColor];
        self.phoneField.font = [UIFont systemFontOfSize:18];
        self.phoneField.returnKeyType = UIReturnKeyNext;
        self.phoneField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        self.phoneField.leftViewMode = UITextFieldViewModeAlways;
        self.phoneField.keyboardAppearance = UIKeyboardAppearanceLight;
        self.phoneField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName : RGBA_HEX(0xffffff, 0.7)}];
        UIButton *phoneClean = [self.phoneField valueForKey:@"_clearButton"];
        [phoneClean setImage:[UIImage imageNamed:@"me_content_but_n"] forState:UIControlStateNormal];
        [self addSubview:self.phoneField];
        [self.phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.equalTo(self.phoneBgView);
            make.right.equalTo(self.codeBtn.mas_left).offset(-10);
            make.left.equalTo(self.phoneBgView.mas_left).offset(20);
        }];
        
//        UIView *line1 = [[UIView alloc] init];
//        line1.backgroundColor = RGB(221, 221, 221);
//        [self.phoneBgView addSubview:line1];
//        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.phoneField.mas_right).offset(10);
//            make.centerY.equalTo(self.phoneField);
//            make.size.mas_equalTo(CGSizeMake(0.5, 15));
//        }];
        
        self.codeBgView = [[UIImageView alloc]init];
        self.codeBgView.backgroundColor = kClearColor;
        [self addSubview:self.codeBgView];
        [self.codeBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.and.height.equalTo(self.phoneBgView);
            make.top.equalTo(self.phoneBgView.mas_bottom);
        }];
        
        self.codeField = [[UITextField alloc]initWithFrame:self.codeBgView.frame];
        self.codeField.font = [UIFont systemFontOfSize:18];
        self.codeField.textColor = [UIColor whiteColor];
        self.codeField.returnKeyType = UIReturnKeyDone;
        self.codeField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.codeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        self.codeField.leftViewMode = UITextFieldViewModeAlways;
        self.codeField.keyboardAppearance = UIKeyboardAppearanceLight;
        self.codeField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"验证码" attributes:@{NSForegroundColorAttributeName : RGBA_HEX(0xffffff, 0.7)}];
        UIButton *codeClean = [self.codeField valueForKey:@"_clearButton"];
        [codeClean setImage:[UIImage imageNamed:@"me_content_but_n"] forState:UIControlStateNormal];
        [self addSubview:self.codeField];
        [self.codeField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.codeBgView).insets(UIEdgeInsetsMake(1, 20, 0, 35));
        }];
        
        UIView *line2 = [[UIView alloc] init];
        line2.backgroundColor = RGBA_HEX(0xCCCCCC,0.5);
        [self addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.phoneField);
            make.right.equalTo(self.codeBtn.mas_left);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(0.5);
        }];

        UIView *line3 = [[UIView alloc] init];
        line3.backgroundColor = RGBA_HEX(0xCCCCCC,0.5);
        [self addSubview:line3];
        [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.phoneBgView.mas_left).offset(25);
            make.right.equalTo(self.phoneBgView.mas_right).offset(-25);
            make.top.equalTo(self.phoneBgView.mas_bottom).offset(1);
            make.height.mas_equalTo(0.5);
        }];

        UIView *line4 = [[UIView alloc] init];
        line4.backgroundColor = RGBA_HEX(0xCCCCCC,0.5);
        [self addSubview:line4];
        [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.codeBgView.mas_left).offset(25);
            make.right.equalTo(self.codeBgView.mas_right).offset(-25);
            make.top.equalTo(self.codeBgView.mas_bottom).offset(1);
            make.height.mas_equalTo(0.5);
        }];
//        self.protocolButton = [[UIButton alloc]init];
//        [self.protocolButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//        [self.protocolButton setTitle:@"登录代表你已同意《服务与隐私条款》" forState:UIControlStateNormal];
//        self.protocolButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        self.protocolButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        [self addSubview:self.protocolButton];
//        [self.protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.codeBgView.mas_bottom).offset(35);
//            make.left.equalTo(self.mas_left).offset(25);
//            make.right.equalTo(self.mas_right).offset(-25);
//            make.height.mas_equalTo(20);
//        }];
        self.loginButton = [[UIButton alloc]init];
        self.loginButton.layer.cornerRadius = 4;
        self.loginButton.layer.masksToBounds = YES;
        self.loginButton.backgroundColor = RGB_HEX(0xFFBB01);
        [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.loginButton setTitleColor:RGBA(255, 255, 255, 0.7) forState:UIControlStateHighlighted];
        [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
        self.loginButton.titleLabel.font = [UIFont systemFontOfSize:18];
        self.loginButton.enabled = NO;
        [self addSubview:self.loginButton];
        [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.codeBgView.mas_bottom).offset(30);
            make.left.equalTo(self.mas_left).offset(25);
            make.right.equalTo(self.mas_right).offset(-25);
            make.height.mas_equalTo(44);
        }];
        self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
        self.codeField.keyboardType = UIKeyboardTypeNumberPad;

        
//        self.YKLoginButton = [[UIButton alloc] init];
//        [self.YKLoginButton setImage:[UIImage imageNamed:@"xl_login_youke_icon"] forState:UIControlStateNormal];
//        [self addSubview:self.YKLoginButton];
//        [self.YKLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(45, 45));
//            make.top.equalTo(self.loginButton.mas_bottom).offset(40);
//            make.centerX.equalTo(self.loginButton.mas_centerX).offset(-50);
//        }];
        
//        if ([WXApi isWXAppInstalled]) {
//            self.WXLoginButton = [[UIButton alloc] init];
//            self.WXLoginButton.backgroundColor = RGB_HEX(0x3CCF7E);
//            self.WXLoginButton.layer.cornerRadius = 4;
//            self.WXLoginButton.layer.masksToBounds = YES;
//            self.WXLoginButton.titleLabel.font = [UIFont systemFontOfSize:18];
//            [self.WXLoginButton setTitle:@" 微信登录" forState:UIControlStateNormal];
//            [self.WXLoginButton setImage:[UIImage imageNamed:@"login_icon_weixin_n"] forState:UIControlStateNormal];
//            [self addSubview:self.WXLoginButton];
//            [self.WXLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.loginButton.mas_bottom).offset(15);
//                make.left.equalTo(self.mas_left).offset(25);
//                make.right.equalTo(self.mas_right).offset(-25);
//                make.height.mas_equalTo(48);
//            }];
//
//        } else {
//        }
    }
    return self;
}
- (void)makeBindUI{
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.backgroundColor = [UIColor whiteColor];
    tipLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightBold];
    tipLabel.textColor = HEXCOLOR(0x000000);
    tipLabel.text = @"检测到游客账号";
    [self addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(35);
        make.top.equalTo(self.mas_top).offset(100);
    }];
    UILabel *tipLabel2 = [[UILabel alloc] init];
    tipLabel2.font = [UIFont systemFontOfSize:13 weight:UIFontWeightBold];
    tipLabel2.textColor = HEXCOLOR(0x999999);
    tipLabel2.text = @"需要绑定手机才能继续使用";
    [self addSubview:tipLabel2];
    [tipLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(35);
        make.top.equalTo(self.mas_top).offset(130);
    }];
    self.bindButton = [[UIButton alloc]init];
    self.bindButton.layer.cornerRadius = 4;
    self.bindButton.layer.masksToBounds = YES;
    self.bindButton.backgroundColor = RGB_HEX(0xFFBB01);
    [self.bindButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.bindButton setTitleColor:RGBA(255, 255, 255, 0.7) forState:UIControlStateHighlighted];
    [self.bindButton setTitle:@"马上绑定" forState:UIControlStateNormal];
    self.bindButton.titleLabel.font = [UIFont systemFontOfSize:18];
    self.bindButton.enabled = NO;
    [self addSubview:self.bindButton];
    [self.bindButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeBgView.mas_bottom).offset(100);
        make.left.equalTo(self.mas_left).offset(25);
        make.right.equalTo(self.mas_right).offset(-25);
        make.height.mas_equalTo(48);
    }];
    _WXLoginButton.hidden = YES;
}
- (void)backButton1{//返回
    _LoginViewBlock();
}
- (UIImage*) buttonImageFromColors:(NSArray*)colors frame:(CGRect)frame {
    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(frame.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    start = CGPointMake(0.0, 0.0);
    end = CGPointMake(frame.size.width, 0.0);
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}
@end
